package com.zhs.gccs.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.zhs.gccs.R;
import com.zhs.gccs.model.CheckBoxModel;
import com.zhs.gccs.utils.OnNoDoubleClickListener;

import java.util.ArrayList;

public class MultSelectPopAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<CheckBoxModel> list;

    public MultSelectPopAdapter(Context context, ArrayList<CheckBoxModel> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder vh;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.mult_pop_list_item, null);
            vh.ll_item =   convertView.findViewById(R.id.ll_item);
            vh.textview = (TextView) convertView.findViewById(R.id.tv_info);
            vh.cb_selected = (CheckBox) convertView.findViewById(R.id.cb_selected);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        CheckBoxModel model = list.get(position);
        vh.textview.setText(model.getTitle());
        vh.cb_selected.setChecked(model.isSelected());
        OnNoDoubleClickListener onNoDoubleClickListener = new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                model.setSelected(!model.isSelected());
                notifyDataSetChanged();
                if(onItemClickListener!=null)onItemClickListener.onItemClick(position);
            }
        };
        vh.ll_item.setOnClickListener(onNoDoubleClickListener);
        return convertView;
    }

    static class ViewHolder {
        View ll_item;
        CheckBox cb_selected;
        TextView textview;
    }
    private  OnItemClickListener onItemClickListener;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }
}
